﻿/// <reference path="jquery-1.4.1.js" />

/** 
 * 日期格式化
 * @params format 格式化样式
 */
Date.prototype.format = function (format) {
    var o =
    {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "S": this.getMilliseconds() //millisecond
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

/**
 * 字符串格式化
 * 
 */
String.prototype.format = function (args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if (args[key] != undefined) {
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    var reg = new RegExp("({[" + i + "]})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}
/** 
 * 获取url参数
 * @returns 返回json对象
 */
function getParams() {
    var url = location.search;
    var params = {};
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            params[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
        }
    }
    return params;
}

/**
 * 数组中查找元素
 * @params array 要查找的数据
 * @params matchFnOrVal 匹配函数或值
 */
function findInArray(array, matchFnOrVal) {
    if (!array.length)
        return null;

    var matchFn = typeof (matchFnOrVal) == "function"
        ? matchFnOrVal
        : function (val) { return val == matchFnOrVal; };

    var resultArr = [];
    for (var i = 0; i < array.length; i++) {
        if (matchFn.call(array[i], array[i])) {
            resultArr.push(array[i]);
        }
    }

    return resultArr;
}

/**
 * 从数组中删除元素
 * @params array 要删除元素的数组,该数组匹配的元素将本删除
 * @params matchFnOrVal 匹配函数或值
 */
function deleteInArray(array, matchFnOrVal) {
    if (!$.isArray(array))
        return null;

    var matchFn = typeof (matchFnOrVal) == "function"
        ? matchFnOrVal
        : function (val) { return val == matchFnOrVal; };

    for (var i = array.length - 1; i >= 0 ; i--) {
        if (matchFn.call(array[i], array[i])) {
            array.splice(i, 1);
        }
    }

    return array;
}

/**
 * 字符串是否相等
 * @param str1
 * @param str2
 * @param careCase 是否区分大小写(默认不区分大小写)
 * @returns 是否相等
 */
function strEquals(str1, str2, careCase) {
    return careCase
        ? str1 == str2
        : (str1 && str2 && str1.toString().toLowerCase() == str2.toString().toLowerCase());
}